package com.jbl.st.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jbl.commonutils.R;
import com.jbl.exceptionhandler.ManagerException;
import com.jbl.st.entity.CourseclassStudent;
import com.jbl.st.entity.Student;
import com.jbl.st.entity.StudentCourseScore;
import com.jbl.st.entity.StudentInfo;
import com.jbl.st.entity.dto.StudentQuery;
import com.jbl.st.entity.vo.CourseclassSelectVO;
import com.jbl.st.entity.vo.StudentVO;
import com.jbl.st.service.CourseclassStudentService;
import com.jbl.st.service.StudentCourseScoreService;
import com.jbl.st.service.StudentInfoService;
import com.jbl.st.service.StudentService;
import com.jbl.te.entity.*;
import com.jbl.te.service.CourseclassService;
import com.jbl.te.service.DepartmentService;
import com.jbl.te.service.DepartmentclassService;
import com.jbl.te.service.MajorService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static com.jbl.commonutils.R.ok;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jbl
 * @since 2020-12-11
 */
@RestController
@CrossOrigin
@RequestMapping("/st/courseclass-student")
public class CourseclassStudentController {

    @Autowired
    DepartmentclassService departmentclassService;
    @Autowired
    CourseclassStudentService courseclassStudentService;
    @Autowired
    StudentService studentService;
    @Autowired
    CourseclassService courseclassService;
    @Autowired
    StudentInfoService infoService;
    @Autowired
    DepartmentclassService classService;
    @Autowired
    DepartmentService departmentService;
    @Autowired
    MajorService majorService;
    @Autowired
    StudentCourseScoreService studentCourseScoreService;



        @PostMapping("/saveDepartmentclassStudent")
    @ApiOperation("通过行政班id添加学生-课程班中间表")
    @Transactional
    public R saveDepartmentclassStudent(String departmentclassId, String courseclassId) {
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.select("id");
        studentQueryWrapper.eq("departmentclass_id",departmentclassId);
        List<Student> studentList = studentService.list(studentQueryWrapper);
        for (Student student : studentList) {
            //通过行政班所有学生id和课程班id添加进中间表
            CourseclassStudent courseclassStudent = new CourseclassStudent();
            courseclassStudent.setCourseclassId(courseclassId);
            courseclassStudent.setStudentId(student.getId());
            courseclassStudent.setState(true);
            boolean save = courseclassStudentService.save(courseclassStudent);
            if(!save) return R.error().message("中途出现行政班学生添加课程班失败！");
        }
        Courseclass courseclass = courseclassService.getById(courseclassId);
        courseclass.setIsConfirm(true);
        courseclassService.updateById(courseclass);
        return R.ok();
    }
    @PutMapping("updateStudentScore")
    @ApiOperation("修改学生")
    public R updateStudentScore(@RequestBody List<StudentVO> vo) {
        vo.forEach(f->{
            StudentCourseScore studentCourseScore =
                    studentCourseScoreService.getOne(new QueryWrapper<StudentCourseScore>().eq(
                            "student_id",f.getId()).eq("courseclass_id",
                            f.getCourseclassId()));
            if(studentCourseScore!=null){//之前有打过分
                studentCourseScore.setScore(Integer.valueOf(f.getScore()));
                studentCourseScoreService.updateById(studentCourseScore);
            }else{
                studentCourseScore =new StudentCourseScore();
                studentCourseScore.setStudentId(f.getId());
                studentCourseScore.setCourseclassId(f.getCourseclassId());
                studentCourseScore.setScore(Integer.valueOf(f.getScore()));
                studentCourseScoreService.save(studentCourseScore);
            }
        });
        return R.ok();
    }
    @GetMapping("/getAllSutdentAndScoreByCourseId")
    @ApiOperation("通过课程班级id获取所有学生以及分数")
    public R getAllStudentAndScoreByCourseId(String id){
        QueryWrapper<CourseclassStudent> courseclassStudentQueryWrapper = new QueryWrapper<>();
        courseclassStudentQueryWrapper.eq("courseclass_id",id);
        List<CourseclassStudent> courseclassStudentList =
                courseclassStudentService.list(courseclassStudentQueryWrapper);
        List<Student> studresult = new ArrayList<>();
        if(courseclassStudentList.size()>0){
            ArrayList idList = new ArrayList();
            for (CourseclassStudent courseclassStudent : courseclassStudentList) {
                idList.add(courseclassStudent.getStudentId());
            }
            QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
            studentQueryWrapper.in("id",idList);
            studresult = studentService.list(studentQueryWrapper);
        }

        List<StudentVO> list = new ArrayList<>();
        for (Student student : studresult) {

            StudentVO vo = new StudentVO();
            BeanUtils.copyProperties(student,vo);

            StudentInfo info = infoService.getById(student.getId());
            BeanUtils.copyProperties(info,vo);

            Department department = departmentService.getById(student.getDepartmentId());
            vo.setDepartmentId(department.getId());
            vo.setDepartmentName(department.getName());
            vo.setCollegeId(department.getCollegeId());

            Major major = majorService.getById(student.getMajorId());
            vo.setMajorId(major.getId());
            vo.setMajorName(major.getName());

            Departmentclass departmentclass = classService.getById(student.getDepartmentclassId());
            vo.setDepartmentclassId(departmentclass.getId());
            vo.setDepartmentclassName(departmentclass.getName());
            StudentCourseScore studentCourseScore =
                    studentCourseScoreService.getOne(new QueryWrapper<StudentCourseScore>().eq(
                            "student_id",student.getId()).eq("courseclass_id",id));
            if(studentCourseScore!=null){
                vo.setScore(String.valueOf(studentCourseScore.getScore()));
            }else{
                vo.setScore("");
            }
            vo.setCourseclassId(id);//临时借用显示为课id
            list.add(vo);
        }
        return  R.ok().data("data",list);
    }
    @GetMapping("/getAllSutdentByCourseId")
    @ApiOperation("通过课程班级id获取所有学生")
    public R getAllSutdentByCourseId(String id){
        QueryWrapper<CourseclassStudent> courseclassStudentQueryWrapper = new QueryWrapper<>();
        courseclassStudentQueryWrapper.eq("courseclass_id",id);
        List<CourseclassStudent> courseclassStudentList =
                courseclassStudentService.list(courseclassStudentQueryWrapper);
        List<Student> studresult = new ArrayList<>();
        if(courseclassStudentList.size()>0){
            ArrayList idList = new ArrayList();
            for (CourseclassStudent courseclassStudent : courseclassStudentList) {
                idList.add(courseclassStudent.getStudentId());
            }
            QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
            studentQueryWrapper.in("id",idList);
            studresult = studentService.list(studentQueryWrapper);
        }

        List<StudentVO> list = new ArrayList<>();
        for (Student student : studresult) {

            StudentVO vo = new StudentVO();
            BeanUtils.copyProperties(student,vo);

            StudentInfo info = infoService.getById(student.getId());
            BeanUtils.copyProperties(info,vo);

            Department department = departmentService.getById(student.getDepartmentId());
            vo.setDepartmentId(department.getId());
            vo.setDepartmentName(department.getName());
            vo.setCollegeId(department.getCollegeId());

            Major major = majorService.getById(student.getMajorId());
            vo.setMajorId(major.getId());
            vo.setMajorName(major.getName());

            Departmentclass departmentclass = classService.getById(student.getDepartmentclassId());
            vo.setDepartmentclassId(departmentclass.getId());
            vo.setDepartmentclassName(departmentclass.getName());

            list.add(vo);
        }
        return  R.ok().data("data",list);
    }

    @DeleteMapping("/delByCourseclassId")
    @ApiOperation("通过课程班id删除所有中间表记录")
    public R delByCourseclassId(String id) {
        QueryWrapper<CourseclassStudent> wrapper = new QueryWrapper<>();
        wrapper.eq("courseclass_id",id);
        boolean remove = courseclassStudentService.remove(wrapper);
        Courseclass courseclass = courseclassService.getById(id);
        courseclass.setIsConfirm(false);
        courseclassService.updateById(courseclass);
        return R.ok();
    }

    @DeleteMapping("/delByCourseclassIdList")
    @ApiOperation("通过课程班id数组删除所有中间表记录")
    @Transactional
    public R delByCourseclassIdList(@RequestBody List<String> idList) {
        for (String id : idList) {
            R r = delByCourseclassId(id);
            if(r.getSuccess() == false) {
                throw new ManagerException(20001,"中途修改失败！");
            }
        }
        return R.ok();
    }

}

